/**
 * <p>Copyright (c) 2011 深圳市鹏途信息技术有限公司 </p>
 * <p>				   All right reserved. 		     </p>
 * 
 * <p>项目名称 ： 	东莞交通工程质量监督综合业务系统        </p>
 * <p>创建者   :	liuyang 
 * 
 * <p>描   述  :   CountersignInfoDefaultImpl.java for com.pengtu.utils.jbpm.rollback.impl    </p>
 * 
 * <p>最后修改 : $: 2011-8-15-上午10:42:00 v 1.0.0	 liuyang   $     </p>
 * 
*/

package com.pengtu.service.custom.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.pengtu.service.custom.*;
/**
 * 
 * CountersignInfoDefaultImpl
 * 
 * 2011-8-15 上午10:42:00
 * 
 * @version 1.0.0
 * 
 */
public class CountersignInfoDefaultImpl implements CountersignInfo {

	/**
	 * serialVersionUID:TODO
	 *
	 * @since 1.0.0
	 */
	
	private static final long serialVersionUID = 1L;

	private List<String> users;  
	  
    private boolean isAllSigned = false;
  
    private Map<String, Conclusion> results = new HashMap<String, Conclusion>();  
  
    private CountersignCalculator countersignCalculator;  
    
    public CountersignInfoDefaultImpl(List<String> users,  
            CountersignCalculator countersignCalculator) {  
        this.users = users;  
        this.countersignCalculator = countersignCalculator;  
    }
	
	/* (non-Javadoc)
	 * @see com.pengtu.utils.jbpm.rollback.CountersignInfo#sign(java.lang.String, com.pengtu.utils.jbpm.rollback.Conclusion)
	 */
	@Override
	public synchronized boolean sign(String user, Conclusion conclusion) {
		// TODO Auto-generated method stub
		if (!users.contains(user)) {  
            return false;  
        }  
        results.put(user, conclusion);  
        if (results.size() == users.size()) {  
            isAllSigned = true;  
        }  
        return true;
	}

	/* (non-Javadoc)
	 * @see com.pengtu.utils.jbpm.rollback.CountersignInfo#getUsers()
	 */
	@Override
	public List<String> getUsers() {
		// TODO Auto-generated method stub
		return Collections.unmodifiableList(users);
	}

	/* (non-Javadoc)
	 * @see com.pengtu.utils.jbpm.rollback.CountersignInfo#getConclusion()
	 */
	@Override
	public Conclusion getConclusion() {
		// TODO Auto-generated method stub
		return countersignCalculator.calculate(this);
	}

	/* (non-Javadoc)
	 * @see com.pengtu.utils.jbpm.rollback.CountersignInfo#isAllSigned()
	 */
	@Override
	public boolean isAllSigned() {
		// TODO Auto-generated method stub
		return isAllSigned;
	}

	/* (non-Javadoc)
	 * @see com.pengtu.utils.jbpm.rollback.CountersignInfo#getUserConclusion(java.lang.String)
	 */
	@Override
	public Conclusion getUserConclusion(String userId) {
		// TODO Auto-generated method stub
		return results.get(userId);
	}

	/* (non-Javadoc)
	 * @see com.pengtu.utils.jbpm.rollback.CountersignInfo#getConclusions()
	 */
	@Override
	public Map<String, Conclusion> getConclusions() {
		// TODO Auto-generated method stub
		return Collections.unmodifiableMap(results);
	}

	/* (non-Javadoc)
	 * @see com.pengtu.utils.jbpm.rollback.CountersignInfo#getConclusionCalculator()
	 */
	@Override
	public CountersignCalculator getConclusionCalculator() {
		// TODO Auto-generated method stub
		return countersignCalculator;
	}

}
